// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BaseView from "../base/BaseView";
import PlateInfoItem from "../base/items/PlateInfoItem";
import TushareModule from "../module/TushareModule";
import VirtualList from "../ui/VirtualList";
import DropList from "./items/DropList";
import { PlateList } from "./models/ThsRankData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class AnalyseStocksView extends BaseView {

    @property(VirtualList)
    virList: VirtualList<PlateInfoItem> = null;

    @property(DropList)
    dropPlate: DropList = null;

    @property(DropList)
    dropRank: DropList = null;

    @property(DropList)
    dropType: DropList = null;

    _results: any = null;

    onLoad () {
        this.dropPlate.setData(['全部','行业','细分','概念'], 0);
        this.dropPlate.setSelectCallback((index)=>{
            this.updateView();
        });
        this.dropRank.setData(['成份股数量','成份股权重','数量/总数','权重/总数'], 1);
        this.dropRank.setSelectCallback((index)=>{
            this.updateView();
        });
        this.dropType.setData(['全部','1','2','3','4','5','6','7','8'], 2);
        this.dropType.setSelectCallback((index)=>{
            this.updateView();
        });
        cc.director.on('PlateItemClickEvent', this.onPlateItemClickEvent, this);
    }

    protected onDestroy(): void {
        cc.director.off('PlateItemClickEvent', this.onPlateItemClickEvent, this);
    }

    onPlateItemClickEvent (stocks) {
        this.createDialog(BaseView.Views.LookStocksView, stocks);
    }

    show(params?: Object): void {
        let result = TushareModule.getInstance().analyseStocksPlate(params['stocks'], params['type']);
        this._results = result;
        this.updateView();
    }

    updateView () {
        let data = [];
        let plate = this.dropPlate.getSelectIndex() - 1;
        let rankIndex = this.dropRank.getSelectIndex();
        let type = this.dropType.getSelectIndex();
        for (let index = 0; index < PlateList.length; index++) {
            const key = PlateList[index];
            let array = this._results[key];
            if (plate == -1 || plate == index) {
                for (let index = 0; index < array.length; index++) {
                    const item = array[index];
                    if (item.stocks.length > type) {
                        let total = TushareModule.getInstance().thsPlateMember[item.ts_code].length;
                        item.count2Total = item.stocks.length / total;
                        item.weight2Total = item.weight / total;
                        item.count = item.stocks.length;
                        item.total = total;
                        data.push(item);
                    }
                }
            }
        }

        let rankKeys = ['count', 'weight', 'count2Total', 'weight2Total'];
        let key = rankKeys[rankIndex];
        data.sort(function (a, b) {
            return b[key] - a[key];
        });
        this.virList.argsArr = data;
    }

    // update (dt) {}
}
